package com.ssyc.gsk_teacher_appraisal.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.manager.OneKeyShareCallback;
import com.ssyc.common.manager.ShareManager;
import com.ssyc.common.utils.SPUtil;
import com.ssyc.common.utils.WebViewUtils;
import com.ssyc.gsk_teacher_appraisal.R;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;

/**
 * 学生试卷分析界面
 * Created by Administrator on 2018/11/23 0023.
 */

public class TkStudentTestPaperAnalyzeActivity extends BaseActivity implements View.OnClickListener {

    //返回
    private ImageView ivBack;

    //标题
    private TextView tvTitle;

    //分享
    private ImageView ivShare;

    //加载视图
    private RelativeLayout rlLoading;

    //内容布局
    private LinearLayout llContent;

    //WebView控件
    private WebView webView;

    //WebView加载的H5地址
    private String loadingUrl = HttpAdress.UPDATE_URL + "/JPXB/sharePage/pages/JPXB/xinerShare/TestShare.html";

    //带参加载和分享的H5地址
    private String url;

    //WebView加载的H5需要传递的参数
    private String exam_id, school_id, ttel, stel, num, row_id, name, rangetag;

    /**
     * 跳转到当前界面的方法
     *
     * @return
     */
    public static void startActivity(Context context, String exam_id, String school_id, String ttel, String stel, String num, String row_id, String name, String rangetag) {
        Intent intent = new Intent(context, TkStudentTestPaperAnalyzeActivity.class);
        intent.putExtra("exam_id", exam_id);
        intent.putExtra("school_id", school_id);
        intent.putExtra("ttel", ttel);
        intent.putExtra("stel", stel);
        intent.putExtra("num", num);
        intent.putExtra("row_id", row_id);
        intent.putExtra("name", name);
        intent.putExtra("rangetag", rangetag);
        context.startActivity(intent);
    }

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_tk_student_test_parper_analyze;
    }

    @Override
    protected void init() {

        showContent();

        //获取传递参数
        initIntent();

        //初始化视图
        initView();

        //设置数据
        initWebView();

        //设置设局
        initData();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        ivShare = findViewById(R.id.iv_share);
        ivShare.setOnClickListener(this);
        rlLoading = findViewById(R.id.rl_loading);
        llContent = findViewById(R.id.ll_content);
        rlLoading.setVisibility(View.VISIBLE);
        llContent.setVisibility(View.INVISIBLE);
        webView = findViewById(R.id.webView);
        tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText(name + "的测评报告");
    }

    /**
     * 获取传递参数
     */
    private void initIntent() {
        Intent intent = getIntent();
        exam_id = intent.getStringExtra("exam_id");
        school_id = intent.getStringExtra("school_id");
        ttel = intent.getStringExtra("ttel");
        stel = intent.getStringExtra("stel");
        num = intent.getStringExtra("num");
        row_id = intent.getStringExtra("row_id");
        name = intent.getStringExtra("name");
        rangetag = SPUtil.getString(this, "shareconent");
    }

    /**
     * 设置WebView
     */
    private void initWebView() {
        WebViewUtils.setWebview(this, webView);
    }

    /**
     * 设置WebView的数据
     */
    private void initData() {
        //拼接加载的Url地址
        url = loadingUrl + "?" + "type=9&" + "exam_id=" + exam_id + "&" + "school_id=" + school_id + "&" + "ttel=" + ttel + "&" + "stel=" + stel
                + "&" + "num=" + num + "&" + "row_id=" + row_id;

        Log.i("test", "url:" + url);

        //加载Url
        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient() {

            //设置不用系统浏览器打开,直接显示在当前Webview
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            //加载结束
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                rlLoading.setVisibility(View.GONE);
                llContent.setVisibility(View.VISIBLE);
            }
        });
    }


    /**
     * 点击事件
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        } else if (id == R.id.iv_share) {
            //分享
            doShareAction();
        }
    }


    //点击返回上一页面而不是退出浏览器
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    //销毁Webview
    @Override
    protected void onDestroy() {
        if (webView != null) {
            webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            webView.clearHistory();

            ((ViewGroup) webView.getParent()).removeView(webView);
            webView.destroy();
            webView = null;
        }
        super.onDestroy();
    }


    /**
     * 分享的方法
     */
    private void doShareAction() {
        //分享地址
        String shareUrl = url;

        //分享图片地址
//        String imgUrl = "http://139.129.57.83:8080/Appandroid/jpxblogo.png";
        String imgUrl = "https://www.ycsjedu.com/share/Appandroid/jpxblogo.png";

        //分享标题
        String title = name + rangetag + "的测评报告";

        if (TextUtils.isEmpty(url)) {
            return;
        }
        ShareManager.newInstance().showOneShare(this, title, shareUrl, "请查收", imgUrl, shareUrl, new OneKeyShareCallback() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                Log.i("test", "分享成功");
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                Log.i("test", "分享失败");
            }

            @Override
            public void onCancel(Platform platform, int i) {

                Log.i("test", "取消分享");
            }
        });
    }

}
